/*
 * SOAPProcessor.java
 * 
 * Version information
 * 
 * Feb 21, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.util;

import java.io.StringWriter;
import java.util.Enumeration;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.apache.soap.Body;
import org.apache.soap.Envelope;
import org.apache.soap.util.xml.DOM2Writer;
import org.apache.xmlbeans.XmlObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author rahul
 *  
 */
public class SOAPProcessor
{
	private static Logger oLogger = Logger.getLogger(SOAPProcessor.class);

	public static Envelope createSOAPEnvelope(XmlObject oXmlObject)
	{
		//TODO
		return null;
	}

	/**
	 * 
	 * @param oBufferedReader
	 * @return
	 */
	public static XmlObject getValueFromSOAPEnvelope(
			Document oDocument)
	{
		try
		{
			if (oDocument == null)
			{
				return null;
			}

			Envelope oEnvelope = Envelope.unmarshall(oDocument
					.getDocumentElement());

			XmlObject oXmlObject = null;
			Body oBody = oEnvelope.getBody();
			Vector oVector = oBody.getBodyEntries();

			StringWriter writer = new StringWriter();

			Element oElement = null;

			for (Enumeration e = oVector.elements(); e.hasMoreElements();)
			{
				oElement = (Element) e.nextElement();
				DOM2Writer.serializeAsXML((Node) oElement, writer);
				break;//there is just one artifact resolve document
			}

			if (oElement != null)
			{
				oXmlObject = XmlObject.Factory.parse((Node) oElement);
			}

			return oXmlObject;

		} catch (Exception ex)
		{
			oLogger.warn("getValueFromSOAPEnvelope() : "+ex.getMessage()); 
			return null;
		}
	}

}
